package com.yungu.swift.api.route.v1.passenger;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.api.base.gateway.GatewayHeader;
import com.yungu.swift.assets.passenger.model.dto.PassengerDto;
import com.yungu.swift.assets.passenger.service.PassengerService;
import com.yungu.swift.autoconfig.redis.RedisStringCacheService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.order.model.dto.OrderDto;
import com.yungu.swift.order.model.param.ApiOrderListParam;
import com.yungu.swift.order.model.vo.*;
import com.yungu.swift.order.service.*;
import com.yungu.swift.system.sys.model.vo.SysPassengerAccountVo;
import com.yungu.swift.system.sys.service.SysAccountPassengerService;
import com.yungu.swift.utils.JsonUtils;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;

import java.util.*;

/**
 * <p>描述：</p>
 *
 * @author xuzhicheng
 * @Time 2016/12/12
 */
@Slf4j
@Controller
@RequestMapping(value = "/api/v1/passenger")
@Api(value = "【乘客】乘客注销接口", tags = "乘客注销操作")
public class CancelApi extends BaseApi {

    @Reference
    private PassengerService passengerService;
    @Reference
    private SysAccountPassengerService sysAccountPassengerService;
    @Reference
    private OrderService orderService;

    @Autowired
    private RedisStringCacheService redisStringCacheService;
    public static final String ACCESS_TOKEN_PREFFIX = "PASSENGER_ACCESS_TOKEN_";
    public static final String REFRESH_TOKEN_PREFFIX = "PASSENGER_REFRESH_TOKEN_";
    public static final String PASSENGER_UUID = "PASSENGER_UUID_";

    @RequestMapping(value = {"/cancel"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "乘客注销接口", notes = "乘客注销", response = ResponseData.class)
    public ResponseData<Boolean> addComment(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "uuid", value = "uuid", required = true) @RequestParam String uuid
    ) {
        //获取当前用户钱包是否为0
        Map<String, Object> map = MapUtils.build();
        map.put("uuid", uuid);
        SysPassengerAccountVo data = sysAccountPassengerService.findAccountPassengerVo(map).getData();
        if (data.getRechargeBalance().compareTo(new BigDecimal("0")) > 0 || data.getPresentBalance().compareTo(new BigDecimal("0")) > 0) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "余额大于0，不能注销");
        }
        //获取当前用户是否有进行中的订单
        ApiOrderListParam apiOrderListParam = new ApiOrderListParam();
        apiOrderListParam.setUserUuid(uuid);
        List<JsonOrderListVo> orderList = orderService.queryOrderDoing(apiOrderListParam).getData();
        if (orderList.size() > 0) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "你有正在进行中的订单，不能注销");
        }
        //获取当前用户是否有未支付的订单
        Map<String, Object> maps = MapUtils.build();
        maps.put("passengerUuid", uuid);
        maps.put("payStatus", "0");
        List<OrderDto> list = orderService.list(maps).getData();
        if (list.size() > 0) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "你有未支付的订单，不能注销");
        }
        maps.put("payStatus", "2");
        List<OrderDto> list1 = orderService.list(maps).getData();
        if (list1.size() > 0) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "你有正在退款中的订单，不能注销");
        }
        //执行注销删除用户操作
        PassengerDto passengerDto = new PassengerDto();
        passengerDto.setUuid(uuid);
        passengerService.del(passengerDto);
        //清空redis缓存
        String jsonTokenMap = redisStringCacheService.get(PASSENGER_UUID + uuid, false);
        if (StringUtils.isEmpty(jsonTokenMap)) {
            return ResponseData.buildSuccessResponse("退出成功", true);
        }
        redisStringCacheService.del(PASSENGER_UUID + uuid, false);
        return ResponseData.buildSuccessResponse("注销成功！", true);
    }

}
